export default {
    data: {
        prize:'带薪上班5日奖!!!',
        num:0,
        sum:0,
        isDraw:true,
        el:'',
        ctx:'',
        mWidth:'',
        mHeight:'',
        percentage:'',
        timeId:undefined,
        isShow:false
    },
    onShow(){
        this.draw();
    },
    draw(){
        var el = this.$refs.canvas;
        var ctx = el.getContext('2d',{ antialias: true });
        this.el = el
        this.ctx = ctx

        //填充的颜色
        ctx.fillStyle = 'gray';
        //填充矩形  fillRect(起始X,起始Y,终点X,终点Y)
        ctx.fillRect(0, 0, 202, 43);

        this.ctx.fillStyle = '#000';
        //绘制填充文字
        this.ctx.font = "28px";
        this.ctx.fillText('刮开有奖', 50, 30);
        console.log('cqtest 0 文字填充完成')

        //得到画布的宽高
        let a =Number(JSON.stringify(this.el.style.width).replace(/[^\d]/g,''));
        let b =Number(JSON.stringify(this.el.style.height).replace(/[^\d]/g,''));
        this.mWidth = a;
        this.mHeight = b;
    },

    touchstart() {
        this.isDraw = true;
    },

    touchmove(e) {
        let x = JSON.stringify(e.touches)
        //去掉中括号,将其变成对象
        let x1 = x.replace(/\[|]/g,'')
        let x2 = JSON.parse(x1)
        let x3 = JSON.stringify(x2)

        //计算触摸点位置
        let X1 = parseInt(JSON.parse(x3).localX)
        let Y1 = parseInt(JSON.parse(x3).localY)

        this.ctx.globalCompositeOperation = 'destination-out';

        //画圆
        this.ctx.arc(X1, Y1, 10, 0, 2 * Math.PI);
        console.log('6666666')
        //填充圆形
        this.ctx.fill();

        this.timeId = setInterval(()=>{
            clearInterval(this.timeId)
            this.percentage = this.getFilledPercentage()
            this.handleFilledPercentage(this.percentage);
        },200)
    },

    touchend() {
        this.isDraw = false;
        clearInterval(this.timeId)
    },

    touchcancel(){
        this.isDraw = false
        clearInterval(this.timeId)
    },

    //计算已经刮过的区域占整个区域的百分比
    getFilledPercentage(){
        let imgData = this.ctx.getImageData(0,0,this.mWidth,this.mHeight);
        //imgData.data是个数组,存储着指定区域每个像素点的信息,数组中4个元素表示一个像素点的rgba值
        let pixels = imgData.data;
        let transPixels = [];
        for(let i=0;i<pixels.length;i+=4){
            //需要判断像素点是否透明需要判断该像素点的a值是否为0
            if(pixels[i+3] == 0){
                transPixels.push(pixels[i+3])
            }
        }
        return (transPixels.length/(pixels.length/4)*100).toFixed(2) + '%'
    },

    //设置阈值,去除灰色涂层
    handleFilledPercentage(percentage){
        percentage = percentage || 0;
        console.log('percentage =' + percentage)
        if(parseInt(percentage)>50){
            //去除画布方法一:直接将canvas涂层清除
            this.ctx.clearRect(0,0,this.mWidth,this.mHeight)
            console.log('清除画布')

            //方法2:将canvas涂层设置为透明
            //this.ctx.fillStyle = 'rgba(255,255,255)';
            //this.ctx.fillRect(0,0,this.mWidth,this.mHeight)
        }
    },

    getRandomInt(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min)
    },

    onDestroy(){
        clearInterval(this.timeId)
    }


}
